using System.Collections.Generic;
using System.Globalization;
using System.IO;
using NFreeMarker.Cache;
using NFreeMarker.Template;
using NUnit.Framework;

namespace NFreeMarker.Tests.Template
{
    public class TemplateTestsBase
    {
        private const string TemplateName = "test.ftl";

        protected Configuration Configuration;
        private StringTemplateLoader _templateLoader;
        private Dictionary<string, object> _root;

        [SetUp]
        public virtual void SetUp()
        {
            _templateLoader = new StringTemplateLoader();

            Configuration = new Configuration();
            Configuration.SetObjectWrapper(new DefaultObjectWrapper());
            Configuration.SetTemplateLoader(_templateLoader);
            Configuration.SetLocale(CultureInfo.GetCultureInfo("en-US"));

            _root = new Dictionary<string, object>();
        }

        protected string ProcessTemplate()
        {
            NFreeMarker.Template.Template template = Configuration.GetTemplate(TemplateName);

            string output;
            using (TextWriter writer = new StringWriter())
            {
                template.Process(_root, writer);
                output = writer.ToString();
            }
            return output;
        }

        protected void SetTemplateContent(string content)
        {
            _templateLoader.PutTemplate(TemplateName, content);
        }

        protected void SetTemplateContent(string templateName, string content)
        {
            _templateLoader.PutTemplate(templateName, content);
        }

        protected void AddTemplateVariable(string name, object value)
        {
            _root.Add(name, value);
        }
    }
}